<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\LogHander;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Middleware;
use App\admin\Anlicategory;
use App\admin\Anli;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Routing\Controller as BaseController;

class AnliController extends BaseController
{
	public function __construct() {
		$this->middleware('admindo');
	}
	//案例-list
	public function index()
	{
//		$Anli = new Anli;$Anlicategory = new Anlicategory;
//		$flights = $Anli->category()->get();
//		dd($flights);
//		return view('admin.anlilist',['data' => $flights]);
//-----------------------------------------------------------------
		$users = DB::table('anlis')
            ->leftJoin('anlicategorys', 'anlis.type', '=', 'anlicategorys.id')
			->orderBy('anlis.id', 'desc')
			->select('anlis.*', 'anlicategorys.catname')
			->paginate(15);
//          ->get();
			return view('admin.anlilist',['data' => $users]);
//-----------------------------------------------------------------
//		$users = $Anli::with(['category'=>function($query){
//		   $query->select('parent_id');
//		}])->get();
//		dd($users);
	}
	//案例-添加
	public function indexadd()
	{
		//取产品分类
		$cate = Anlicategory::orderBy('id', 'desc')->get();
		return view('admin.anliadd',['cat'=>$cate]);
	}
	//案例-添加上传
	public function indexaddUp(Request $request)
	{
		$newName = $request->input('newName');
		$selt = $request->input('selt');
		$Description = $request->input('newDescription');
		$content = $request->input('content');
		
		if($newName == "" || $Description=="" ||$content==""){
			return "请输入案例名称、描述、内容";
		}
		$pathBaseOne = "";
		if ($request -> hasFile('fileOne')) {
			$file = $request -> file('fileOne');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/anli/'. $name;
			Image::make($file) -> save($path);
			$pathBaseOne = 'upload/anli/'. $name;
		}else{
			return "请选择案例导读图片";
		}

		$anli = new Anli;
		$anli->name = $newName;
		$anli->description = $Description;
		$anli->info = $content;
		
		$anli->imgurl = $pathBaseOne;
		$anli->type = $selt;
		$anli->state = 0;
		if($anli->save()){
			$cars=array('name'=>'indexaddUp','info'=>'案例-添加上传','cnt'=>$newName);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('AnliList');
		}
	}

	//案例-编辑
	public function indexedite(Request $request)
	{
		$id = $request->input('id');
		$cate = Anlicategory::orderBy('id', 'desc')->get();
		$Anli = new Anli;
		$temp = $Anli::find($id);
		return view('admin.anliedite',['cat'=>$cate,'data'=>$temp]);
	}
	
	//案例-编辑上传
	public function indexediteUp(Request $request)
	{
		$Anli = new Anli;
		$id = $request->input('id');
		if($id == ""){
			return "更新出错，找不到案例id";
		}
		$newName = $request->input('newName');
		$selt = $request->input('selt');
		$Description = $request->input('newDescription');
		$content = $request->input('content');
		$pathBaseOne = "";
		if ($request -> hasFile('fileOne')) {
			$file = $request -> file('fileOne');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/anli/'. $name;
			Image::make($file) -> save($path);
			$pathBaseOne = 'upload/anli/'. $name;
		}
		//---------------------------------------
		$temp = $Anli::find($id);
		$temp->name = $newName;
		$temp->description = $Description;
		$temp->info = $content;
		
		$temp->imgurl = $pathBaseOne;
		$temp->type = $selt;
		$temp->state = 0;
		if($temp->save()){
			$cars=array('name'=>'anliUpdate','info'=>'编辑产品','cnt'=>$newName);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('AnliList');
		}
	}
	
	public function detial(Request $request)
	{
		$Anli = new Anli;
		$id = $request->input('id');
		if($id == ""){
			return "更新出错，找不到案例id";
		}
		$temp = $Anli::find($id);
		return view('admin.anlidetial',['data'=>$temp]);
	}
	
	public function del(Request $request)
	{
		$id = $request->input('id');
		$Anli = new Anli;
		$data = $Anli::find($id);
		
		$cars=array('name'=>'Anlidel','info'=>'删除案例','cnt'=>$data->name."---".$data->description);
		$temp = new LogHander;
		$temp->logs($cars);
		
//		$data->state = 1;
		if($data->delete()){
			return "ok";
		}else{
			return "no";
		}
	}
	//------------------------------------------------------------------------------
	
	//案例-分类list
	public function category()
	{
		$flight = Anlicategory::orderBy('id', 'desc')->paginate(10);
		return view('admin.anlicategory',['data' => $flight]);
	}
	//案例-添加分类
	public function categoryadd()
	{
		$cate = Anlicategory::orderBy('id', 'desc')->get();
		return view('admin.anlicategoryadd',['cate'=>$cate]);
	}
	//案例-添加分类上传
	public function categoryup(Request $request)
	{
		$catNameT = $request->input('catNameT');
		$catInfoT = $request->input('catInfoT');
		$catSelt = $request->input('selt');
		
		if(!isset($catNameT) || !isset($catInfoT)){
			return "上传参数为空";
		}
		$Cate = new Anlicategory;
		if ($request -> hasFile('fileTwo')) {
			$file = $request -> file('fileTwo');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/anli/'. $name;
			Image::make($file) -> save($path);
			$pathBase = 'upload/anli/'. $name;
			$Cate->imgurl = $pathBase;
		}else{
			return "请上传图片";
		}
		$Cate->catname = $catNameT;
		$Cate->description = $catInfoT;
		$Cate->parent_id = $catSelt;
		$Cate->type = 1;
		$Cate->state = 0;
		$str = json_encode($Cate);
		if($Cate->save()){
			$cars=array('name'=>'categoryup','info'=>'案例分类','cnt'=>$str);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('Anlicategory');
		}
	}
	public function categorydel(Request $request)
	{
		$id = $request->input('id');
		$cate = new Anlicategory;
		$data = $cate::find($id);
		
		$count = Anli::where('type',$id)->count();
		if($count > 0){
			return "cannot";
		}
		if($data->delete()){
			return "ok";
		}else{
			return "no";
		}
	}
}
